package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ActivationCodes;
import com.ruoyi.system.domain.vo.BeneficiariesVo;
import com.ruoyi.system.service.IActivationCodesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Beneficiaries;
import com.ruoyi.system.service.IBeneficiariesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 权益人管理Controller
 *
 * @author ruoyi
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/system/Beneficiaries")
public class BeneficiariesController extends BaseController
{
    @Autowired
    private IBeneficiariesService beneficiariesService;

    @Autowired
    private IActivationCodesService activationCodesService;

    /**
     * 查询权益人管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:list')")
    @GetMapping("/list")
    public TableDataInfo list(Beneficiaries beneficiaries)
    {
        startPage();
        //拿到了权益人表里的数据 现在需要根据权益人表里的激活码字段去查激活码表并将数据返回到权益人表的一个数组里
        List<Beneficiaries> list = beneficiariesService.selectBeneficiariesList(beneficiaries);
        List<BeneficiariesVo> list2 = new ArrayList<>();
        list.forEach(str ->{
//            System.out.println(Arrays.toString(activationCodeArray));
            //循环该数组 使用该激活码去另一个service中获取该条数据的内容
                System.out.println(str.getActivationCode());
                ActivationCodes activationCodes = activationCodesService.selectActivationCodesByActivationCodes(str.getActivationCode());
                System.out.println(activationCodes);
                BeneficiariesVo beneficiariesVo = new BeneficiariesVo();
                beneficiariesVo.setId(str.getId());
                beneficiariesVo.setName(str.getName());
                beneficiariesVo.setIdType(str.getIdType());
                beneficiariesVo.setIdNumber(str.getIdNumber());
                beneficiariesVo.setPhoneNumber(str.getPhoneNumber());
                beneficiariesVo.setCity(str.getCity());
                beneficiariesVo.setAgentName(str.getAgentName());
                beneficiariesVo.setAgentId(str.getAgentId());
                beneficiariesVo.setActivationCode(str.getActivationCode());
                beneficiariesVo.setActivationCodes(activationCodes);
                list2.add(beneficiariesVo);

        });
        return getDataTable(list2);
    }

    /**
     * 导出权益人管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:export')")
    @Log(title = "权益人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Beneficiaries beneficiaries)
    {
        List<Beneficiaries> list = beneficiariesService.selectBeneficiariesList(beneficiaries);
        ExcelUtil<Beneficiaries> util = new ExcelUtil<Beneficiaries>(Beneficiaries.class);
        util.exportExcel(response, list, "权益人管理数据");
    }

    /**
     * 获取权益人管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(beneficiariesService.selectBeneficiariesById(id));
    }

    /**
     * 新增权益人管理
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:add')")
    @Log(title = "权益人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Beneficiaries beneficiaries)
    {
        return toAjax(beneficiariesService.insertBeneficiaries(beneficiaries));
    }

    /**
     * 修改权益人管理
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:edit')")
    @Log(title = "权益人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Beneficiaries beneficiaries)
    {
        return toAjax(beneficiariesService.updateBeneficiaries(beneficiaries));
    }

    /**
     * 删除权益人管理
     */
    @PreAuthorize("@ss.hasPermi('system:Beneficiaries:remove')")
    @Log(title = "权益人管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(beneficiariesService.deleteBeneficiariesByIds(ids));
    }
}
